﻿/**
* @authors <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package {
	
	import com.ng.ResHelp.c_ResHelper;
	import com.ng.ResHelp.c_ResLoader;
	import com.ng.utils.c_Notification;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.System;
	
	//Important: forced import for classes that are not explicitly used in code but only via XML-declared method with ResHelp
	c_PhysCircleAnimatedLips;
	
	[SWF(width="600",height="400",backgroundColor="#FFFFFF",framerate="30")]
	public class TestResHelp	extends MovieClip{

		[Embed(source = "../data/settings.xml", mimeType = "application/octet-stream")]
		private var embedXML:Class;		
		
		private var __ResLoader:c_ResLoader;
		private var __ResHelper:c_ResHelper;
		
		var __notification:c_Notification;
		
		public function TestResHelp()  {
			__ResLoader = c_ResLoader.GetInstance();
			
			//Two ways to load the config XML
			//1, embed
			//var xmldata:Object = new embedXML();
			//__ResLoader.initXML(xmldata);			
			//2, normal load
			__ResLoader.init("data/settings.xml");			
			__ResLoader.addEventListener(c_ResLoader.EXTERNAL_LIBRARIES_LOADED, OnSWFAutoLoaded);
		}		
		private function OnSWFAutoLoaded(e:Event) {
			trace('OnSWFAutoLoaded:' + e.type);
			__ResLoader.removeEventListener(c_ResLoader.EXTERNAL_LIBRARIES_LOADED, OnSWFAutoLoaded);
			
			//want delay option? let's go ...
			__ResLoader.initLoadingDelayChannel();
			__ResLoader.addEventListener(c_ResLoader.EXTERNAL_LIBRARIES_LOADED, OnSWFDelayLoaded);
		}
		private function OnSWFDelayLoaded(e:Event):void {
			trace('OnSWFDelayLoaded:' + e.type);
			__ResLoader.removeEventListener(c_ResLoader.EXTERNAL_LIBRARIES_LOADED, OnSWFDelayLoaded);
			__ResHelper = c_ResHelper.GetInstance();
			__ResHelper.init(__ResLoader);
			init();
			//IMPORTANT: can do same thing with __ResLoader.InitLoadingOnDemandChannel();
			testElelements();
		}
		private function init():void {
			/**		 
			//This is a test for loading one element using class
			var cl:Class = __ResHelper.getClassById("PhysBox");
			trace("loaded: "+cl);
			addChild(new cl());
			*/
			//let's get the whole frame setup
			var f:Sprite = __ResHelper.getFrameById('frame1');
			if (f !=null) {
				addChild(f);
			}
			addChild(__notification = new c_Notification(10, 10, 200, 40));
			__notification.Update('Total memory: '+ System.totalMemory.toString() +' bytes' );
		}
		private function testElelements():void {
			trace(this.getChildByName('frame1').getChildByName('elem4composition').getChildByName('elem4component1'));
			
			//IMPORTANT: this is another way to add behaviors to loaded element without using the controler class
			//(see c_PhysCircleAnimatedLips.as which controls the PhysCircleAnimatedLips object from defaultLibrary.swf)				
			this.getChildByName('frame1').getChildByName('elem4composition').getChildByName('elem4component1').addEventListener(Event.ENTER_FRAME, eyesMoving);
			var go_right:Boolean = true;
			var radian:int = 15;
			function eyesMoving(e:Event) {
				if (go_right) {
					e.target.x ++; 
				} else {
					e.target.x --; 
				}
				if (e.target.x > radian) {
					go_right = false;
				} else if (e.target.x < -radian) go_right = true;
			}			
		}

	}
}